<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>视频添加</legend>
            <div>
                <form class="layui-form layui-form-pane" action="" lay-filter="vodadd">

                    <div class="layui-form-item">
                        <label class="layui-form-label">视频标题</label>
                        <div class="layui-input-inline">
                            <input type="text" name="title" autocomplete="off" placeholder="请输入视频标题"
                                class="layui-input">
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">来源</label>
                            <div class="layui-input-inline">
                                <select name="soform" lay-filter="aihao">
                                    {volist name=":putSearchList()" id="data"}
                                    <option value="{:config($data.val.'.sign')}">{:config($data.val.'.title')}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary" lay-submit
                                lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>

                    <div class="layui-form-item" id="searchResult"></div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">海报地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="pic" lay-verify="title" autocomplete="off" placeholder="请输入轮播图片地址"
                                class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">播放地址</label>
                        <div class="layui-input-block">
                            <textarea id="playurl" placeholder="请输入播放地址" name="playurl"
                                class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">视频简介</label>
                        <div class="layui-input-block">
                            <textarea id="playurl" placeholder="请输入播放地址" name="playurl"
                                class="layui-textarea"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">视频排序</label>
                        <div class="layui-input-inline">
                            <input type="text" name="sort" lay-verify="title" autocomplete="off" placeholder="数字越大越靠前"
                                class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">视频评分</label>
                        <div class="layui-input-inline">
                            <input type="text" name="sort" lay-verify="title" autocomplete="off" placeholder="请输入视频评分分数"
                                class="layui-input">
                        </div>
                    </div>

                    <!-- <div class="layui-form-item">
                        <label class="layui-form-label">轮播类型</label>
                        <div class="layui-input-block">
                            <input type="radio" name="from" value="直链" title="直链">
                            <input type="radio" name="from" value="站内" title="站内">
                        </div>
                    </div> -->


                    <div class="layui-form-item">
                        <label class="layui-form-label">视频上架</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="close" lay-skin="switch" lay-text="ON|OFF">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <button type="button" class="layui-btn" lay-submit="" lay-filter="vodadd">添加</button>
                    </div>
                </form>
            </div>
        </fieldset>
    </div>


</div>

<script id="searchResults" type="text/html">

    <div class="layui-inline">
        <label class="layui-form-label">搜索结果</label>
        <div class="layui-input-inline">
            <select name="opttitle" lay-filter="aihao">
                {{#  layui.each(d.data, function(index, item){ }}
                <option value="{{index}}">{{item.vod_title}}</option>
                {{#  }); }}
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="get-data-search-btn">获取数据</button>
    </div>

</script>

<script>
    layui.use(['form', 'laytpl'], function () {
        var form = layui.form
            , layer = layui.layer,
            laytpl = layui.laytpl;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        var search_data = [];

        var urlTypeSw = true;

        //监听指定开关
        form.on('switch(urlType)', function (data) {
            // 判断是否可以开启
            if (urlTypeSw == false) {
                this.checked = false;
                form.render();
                layer.msg('当前地址非直链无法切换');
            }
        });

        // 监听地址输入
        $("#playurl").on("input", function (e) {
            var strRegex = "^((https|http|ftp|rtsp|mms)?://)";
            var re = new RegExp(strRegex);
            urlTypeSw = re.test(e.delegateTarget.value)
            form.val('banner', {
                "urlType": urlTypeSw
            })
            form.render();
        });

        //监听提交
        form.on('submit(addbanner)', function (data) {
            $.ajax({
                url: "/ask/action/system/banner/add",
                type: "post",
                timeout: 10000,
                dataType: "json",
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                data: data.field,
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg(data.msg, { time: 1500 }, function () {
                            window.location = '/{:config("adminPath")}/index#/bannerlist';
                        });
                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function (XMLResponse) {
                    //异常捕获
                    layer.msg(XMLResponse.responseText);
                }
            });
            return false;
        });

        //监听搜索
        form.on('submit(data-search-btn)', function (data) {
            if (data.field.title == "") {
                layer.msg("标题不能为空");
                return;
            }
            var index = layer.load(0, { shade: false }); //0代表加载的风格，支持0-2
            var info = new FormData();
            info.append('wd', data.field.title);
            info.append('from', data.field.soform);
            $.ajax({
                url: "/search/index.json",
                type: "post",
                timeout: 10000,
                dataType: "json",
                contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                data: info,
                contentType: false, //不设置内容类型
                processData: false, //不处理数据
                success: function (data) {
                    data = JSON.parse(data);
                    if (data.code == 200) {
                        search_data = data.data;
                        var getTpl = searchResults.innerHTML
                            , view = document.getElementById('searchResult');
                        laytpl(getTpl).render(data, function (html) {
                            view.innerHTML = html;
                            form.render();
                        });
                        layer.close(index);
                        layer.msg("搜索到相关信息" + data.number + "条", { time: 1000 });
                    } else {
                        layer.msg("无相关信息");
                    }
                },
                error: function (XMLResponse) {
                    //异常捕获
                    layer.msg(XMLResponse.responseText);
                }
            });
            return false;
        });

        //监听赋值
        form.on('submit(get-data-search-btn)', function (data) {
            var i = data.field.opttitle;
            //表单赋值
            form.val('banner', {
                "title": search_data[i]['vod_title'],//标题
                "pic": search_data[i]['vod_pic'],//海报
                "playurl": search_data[i]['vod_type_id'] + '/' + search_data[i]['vod_url'],//播放地址
                "urlType": false
            })
            urlTypeSw = false;
            return false;
        });



    });
</script>